(function(pageName){
  sugarcane.module[pageName] = function(){
    $('.j-pagescreen').addClass('fn-hide');
    var urlParams = sugarcane.getUrlParams(),page = 1,
      docType = 1,
      tag = urlParams['tag'];
      document.title = tag;
    if ($('#j-screen-'+pageName+tag).length) {
      var pageScreen = $('#j-screen-'+pageName+tag);
      pageScreen.removeClass('fn-hide');
      var _scrollTop = pageScreen.data('scrolltop') || 0;
      setTimeout(function(){
        $(window).scrollTop(_scrollTop);
      }, 10);
      return false;
    }
    var pageScreen = $('<div class="j-pagescreen" id="j-screen-'+pageName+tag+'"></div>');
    $('body').append(pageScreen);

    var listObj = $('<div></div>'),
      moreObj = $('<div class="app-listmore"></div>');
    pageScreen.append(listObj);
    pageScreen.append(moreObj);

    var listPage = 0,
      listLoading = false;

    newslistShow(1);

    moreObj.on(tap, function(){
      if (!listLoading) {
        moreObj.html('正在加载更多...');
        page ++;
        newslistShow(page);
      }
    });

    // 获取夜读/早餐新闻列表
    function newslistShow(page){
      listLoading = true;
      listPage ++;
      sugarcane.ajaxApi({
        "url": "/news/getReadNewsList",
        "data": {"tag": tag, "current": page},
        "callback": function(res){
          listLoading = false;
          if (res['code'] == 0) {
            if (res['data']) {
              var listData = res['data'];
              // sugarcane.wxShare(listData['title'], '分享来自经济日报客户端，更多精彩请下载http://d.jingjiribao.cn/index.html',listData['shareImage'], listData['shareUrl']);
              sugarcane.twoShare({
                "id": res.data.id,
                "title": res.data.title,
                "desc": '分享来自经济日报客户端，更多精彩请下载http://d.jingjiribao.cn/index.html',
                "imgUrl": res.data.shareImage,
                "link": res.data.shareUrl,
                "docType": res.data.docType
              });
              if(res.data.docType){
                docType = res.data.docType;
              }
              sugarcane.module.load('listitem', function(listitemFn){
                $.each(listData, function(index){
                  var _obj = listitemFn(this);
                  listObj.append(_obj);
                  // 点击新闻详情
                  var _detailHref = 'detail.html?id='+this['id'];
                  var _this = this;
                  _obj.on(tap, function(){
                    pageScreen.data('scrolltop', $(window).scrollTop());
                    if(sugarcane['app']['isShare']){
                      sugarcane.route.location({
                        'href': _detailHref
                      });
                    }else{
                      peach.openNews(_this);
                    }

                  });
                });
              });

            }
            if (res['hasMore']) {
              moreObj.html('点击加载更多');
            } else {
              moreObj.remove();
            }
            // shareBanner();
            sugarcane['shareBanner']({
              //'id': id,
              'docType': docType,
              'containObj': pageScreen 
            });
          } else {
            showerrorView();
            peach.hiddenAppbar();
            // alert(res['message']);
          }
        }
      });
    }

    function shareBanner(){
      if(sugarcane['app']['isShare'] && !pageScreen.find('.app-banner').length ){
        sugarcane.module.load('banner', function(listitemFn){
          var bannerObj = listitemFn({'docType': docType});
          pageScreen.append(bannerObj);
          var _height = $(bannerObj).css('height');
          pageScreen.css('margin-bottom',_height);
        });
      }
    }

    function showerrorView(){
      var clientHeight = document.documentElement.clientHeight;
      pageScreen.css('position','relative').css('height',clientHeight+'px');
      var errorviewObj = $(`<div class="app-errorview">
          <img class="img" src="/static/img/isempty.png">
        <p class="error-tip">暂时没有数据</p>
      </div>`);
       pageScreen.append(errorviewObj);
    }

  };

  sugarcane.module['state'][pageName] = 'load';
  sugarcane.module.callback(pageName);
}('readnewslist'));
